package Cantella::DJabberd::Web::UI::ViewPort::Field::RelatedObject::Linked;

use Reaction::Class;
use aliased 'Reaction::UI::ViewPort::Action::Link';

extends 'Reaction::UI::ViewPort::Field::RelatedObject';

has '+value' => ( isa => 'Reaction::InterfaceModel::Object' );

has link => ( is => 'rw', isa => Link );
has controller => ( is => 'ro', isa => 'Str' );
has prototype => ( is => 'rw', isa => 'CodeRef');

has action =>
  (
   is => 'ro',
   isa => 'Str',
   required => 1,
   default => sub{ 'view' },
  );

sub BUILD {
  my $self = shift;
  if( !$self->has_prototype && $self->has_controller ){
    my $controller = $self->controller;
    my $action = $self->action;

    my $proto =  sub {
      [ "${controller}", "${action}", [ $_[0]->__id ] ],
    };
    $self->prototype($proto);
  }
  if( $self->has_prototype ){
    my $link = Link->new(
                        ctx => $self->ctx,
                        location => join('-', $self->location, 'link'),
                        label => $self->value_string,
                        target => $self->value,
                        action => $self->prototype,
                       );
    $self->link($link);
  }
}

__PACKAGE__->meta->make_immutable;

1;
